﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using MangaGrr.DomainModel;
using MinhUtils;

namespace MangaGrr.Logic
{
    public class MangaDownloader
    {
        public event EventHandler LoadChapterCompleted;

        private Manga _manga;
        private List<MangaChapter> _chapterList = null; //is sortable
        private HashSet<string> _chapterUrlSet = null; //to ensure no duplication
        private int _chapterNumber = 0;

        public MangaDownloader(Manga manga)
        {
            _manga = manga;
        }

        internal ICollection<MangaChapter> LoadChapter()
        {
            if (_chapterList == null)
            {
                _chapterList = new List<MangaChapter>();
                _chapterUrlSet = new HashSet<string>();

                LoadChapterFromSite(_manga.URL);
                _chapterList.Sort();
            }

            if (LoadChapterCompleted != null)
                LoadChapterCompleted.Invoke(this, new EventArgs());
            return _chapterList;
        }

        private void LoadChapterFromSite(string url)
        {
            string pageContent = NetUtil.TryDownloadContent(url, 1000);

            //get list of chapters
            MatchCollection mcl = Regex.Matches(pageContent, _manga.MangaSite.ChapterPattern);

            foreach (Match m in mcl)
            {
                string chapterInfo = m.Value;

                //MangaChapter chapter = MGRUtil.IsZenManga(this.MangaSite) ? new MangaChapterZenManga() : new MangaChapter();
                MangaChapter chapter = new MangaChapter();

                chapter.URL = NetUtil.ToAbsoluteURL(StringUtil.PickURL(chapterInfo), _manga.URL);

                if (_chapterUrlSet.Contains(chapter.URL) == false) //if is not duplicated
                {
                    _chapterNumber++;

                    if (string.IsNullOrEmpty(_manga.MangaSite.ChapterOrderPattern)) //no pattern
                        chapter.OrderInfo = MGRUtil.PadZeros(_chapterNumber.ToString()); //use auto-increasing number
                    else
                        chapter.OrderInfo = MGRUtil.PadZeros(Regex.Match(chapterInfo, _manga.MangaSite.ChapterOrderPattern).Value);

                    if (string.IsNullOrEmpty(_manga.MangaSite.ChapterNamePattern)) //no pattern
                        chapter.Name = string.Empty;
                    else
                        chapter.Name = StringUtil.PickValue(chapterInfo, _manga.MangaSite.ChapterNamePattern); //REMOVE LATER
                    //chapter.Name = Regex.Match(chapterInfo, this.MangaSite.ChapterNamePattern).Value;

                    chapter.Manga = _manga;

                    _chapterList.Add(chapter);
                    _chapterUrlSet.Add(chapter.URL);
                }
            }

            if (string.IsNullOrEmpty(_manga.MangaSite.ChapterNextPagePattern) == false) //if paging is implemented
            {
                //get next page url
                Match match = Regex.Match(pageContent, _manga.MangaSite.ChapterNextPagePattern);

                if (match.Value != string.Empty)
                {
                    string nextPageUrl = NetUtil.ToAbsoluteURL(StringUtil.PickURL(match.Value), url);

                    //get list of chapters
                    //again until there's no more next page url can be found
                    LoadChapterFromSite(nextPageUrl);
                }
            }
        }
    }
}
